//mbox helper functions - START

function fnTTMboxLoadDef(mbxName){
	if (typeof mbxName === "undefined" || mbxName =="" ){
		return false;
	}
	//Show/Hide particular mbox'es default/T&T content on match
	mboxFactoryDefault.getMboxes().each(function(i,e) { 
		if (i.getName()==mbxName) {
			
			//get ttHighlMbox and ttHighLLabelWrap
			$ttHighLMboxRefV = $(i.getDiv());
			$ttHighlMboxV = $(i.getDiv()).closest('.ttHighlMbox');
			$ttHighLLabelWrapV = $ttHighlMboxV.find('.ttHighLLabelWrap');
			if (typeof $ttHighLMboxRefV.data('defMboxHTML') === "undefined" && typeof $ttHighLMboxRefV.data('ttMboxHTML') === "undefined") { //first event for this mbox
				//lets save TT and Default HTML as JQ Data
				$ttHighLMboxRefV.data('defMboxHTML',$(i.getDefaultDiv()).html());
				$ttHighLMboxRefV.data('ttMboxHTML',$(i.getDiv()).html());
			}else{
				//we have mbox data stored
			}
			
			if ($(i.getDiv()).find('.ttHighLOfferRestoreDefCont').hasClass('ttoffer')){
				//Restore T&T mbox
				$(i.getDiv()).html($ttHighLMboxRefV.data('ttMboxHTML'));
				$(i.getDiv()).find('.ttHighLOfferRestoreDefCont').removeClass('ttoffer').attr('title','Click this to display the default offer').html('Show default offer');
				
			}else {
				//Restore Default mbox
				$(i.getDiv()).html($ttHighLLabelWrapV.clone().wrap('<p>').parent().html() + $ttHighLMboxRefV.data('defMboxHTML'));
				
				//cleanup non-default info as we're now showing default
				$(i.getDiv()).find('.ttHighLOfferLabel').html('offer: default');
				$(i.getDiv()).find('.ttHighLOfferRestoreDefCont').addClass('ttoffer').attr('title','Click this to display the previously loaded T&T offer').html('Show T&amp;T offer');				
			}


		}
	});
}

//Dialog show/hide function
function ttCustomMboxHTMLOverlay() {
	$dialCustomHTMLOfferOverlay = $("#ttCustomMboxHTMLOverlay");
	$dialCustomHTMLOfferContent = $("#ttCustomMboxHTMLWrap");
	$dialCustomHTMLOfferOverlay.toggle();
	$dialCustomHTMLOfferContent.slideToggle('fast');
}

function fnTTMboxLoadCustom(mbxName){
	if (typeof mbxName === "undefined" || mbxName =="" ){
		return false;
	}
	$('#ttCustomMboxHTMLWrap').remove(); //clean up previous version
	var exstHTML = '';
	
	//Grab the HTML for particular mbox on match
	mboxFactoryDefault.getMboxes().each(function(i,e) { 
		if (i.getName()==mbxName) {
			$ttHighLMboxRefV = $(i.getDiv());
			if (exstHTML == "") { //first event for this mbox
				if($(i.getDiv()).find('.ttHighLOfferRestoreDefCont').length ==1){
					//non-default offer on load
					if($(i.getDiv()).find('.ttHighLOfferRestoreDefCont.ttoffer').length !== 1){
						//T&T content is displayed
						$iGetDivClone = $(i.getDiv()).clone();
						//clean up elements that don't belong to the offer code
						$iGetDivClone.find('.ttHighLLabelWrapDef').remove();
						$iGetDivClone.find('.ttHighLLabelWrap').remove();
						$iGetDivClone.find("div[id^='mboxClick']").find(':first').unwrap(); //unwrap mbox elements
						//TODO - filter doesn't work well with inline CSS
						//exstHTML = $($iGetDivClone.html()).filter('*').html(); //filter would remove comments and script tags
						exstHTML = $iGetDivClone.html();
						//cleanup clone
						$iGetDivClone.remove();
					}else{
						//default content is displayed
						$iGetDivClone = $(i.getDefaultDiv()).clone();
						//clean up elements that don't belong to the offer code
						$iGetDivClone.find('.ttHighLLabelWrapDef').remove();
						$iGetDivClone.find('.ttHighLLabelWrap').remove();
						exstHTML = $iGetDivClone.html();
						//cleanup clone
						$iGetDivClone.remove();
					}
				}else{
					//default offer on load
					$iGetDivClone = $(i.getDiv()).clone();
					//clean up elements that don't belong to the offer code
					$iGetDivClone.find('.ttHighLLabelWrapDef').remove();
					$iGetDivClone.find('.ttHighLLabelWrap').remove();
					exstHTML = $iGetDivClone.html();
					//cleanup clone
					$iGetDivClone.remove();
				}
			}else{
				//we have mbox data stored
			}			
		}
	});	
	
	$("html > body").append('<div id="ttCustomMboxHTMLOverlay">&nbsp;</div><div id="ttCustomMboxHTMLWrap"><div id="ttCustomMboxHTMLTitle"><span class="tt-title">Custom Offer HTML</span><button class="tt-title-close-btn" role="button" title="close"><span class="tt-closethick"></span><span class="tt-title-close-text">close</span></button></div><div id="ttCustomMboxHTMLContent">Please paste the custom HTML below, and click on the Apply button<textarea placeholder="Custom HTML goes here. Type/Paste to clear this text...">'+exstHTML+'</textarea><button type="button">Apply</button></div></div>');
	
	//Dialog Style 
	
	$('#ttCustomMboxHTMLOverlay').css({display: 'none',	position: 'fixed', left: '0px', top: '0px', width:'100%', height: ($(document).height())+'px', textAlign: 'center', zIndex: '1000', background: '#aaaaaa url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png) 50% 50% repeat-x', opacity: '0.3', filter: 'Alpha(Opacity=30)'});
	 
	  $('#ttCustomMboxHTMLWrap').css({display: 'none', position: 'fixed', left: (Math.floor(window.innerWidth/2)-350) +'px', top: '50px', width: '700px', float: 'left',	border: '1px solid #000', padding: '3px', textAlign: 'left', fontSize: '12px', fontFamily: 'Verdana,Arial,sans-serif', borderRadius: '4px', border: '1px solid #aaaaaa', background: '#ffffff url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-bg_flat_75_ffffff_40x100.png) 50% 50% repeat-x', zIndex: '99999'});
	  
	  $('#ttCustomMboxHTMLContent').css({margin: '10px 0', padding: '5px', clear: 'both', lineHeight: '1.5'});
	   $('#ttCustomMboxHTMLContent textarea').css({display: 'block', margin: '10px 0', padding: '5px', clear: 'both', width: '675px', height: '200px', textAlign: 'left', fontFamily: '"Courier New", Courier, monospace', color: '#4169e1'});
	   
	   $('#ttCustomMboxHTMLContent button').css({display: 'block', padding: '5px 10px', cursor: 'pointer'});
	  
	  $('#ttCustomMboxHTMLTitle').css({color: '#fff', height: '20px', padding: '0.4em 1em', position: 'relative', borderRadius: '4px', fontWeight: 'bold',	border: '1px solid #aaaaaa', background: '#00aeef'});
	  
	  $('#ttCustomMboxHTMLTitle .tt-title').css({float: 'left',	margin: '0.1em 0', whiteSpace: 'nowrap', width: '90%', overflow: 'hidden', textOverflow: 'ellipsis', fontWeight: 'bold', color: '#fff'});
	  
	  $('#ttCustomMboxHTMLTitle .tt-title-close-btn').css({border: '1px solid #d3d3d3', background: '#e6e6e6 url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png) 50% 50% repeat-x', color: '#555555',	fontWeight:'normal', fontSize: '1em',	position: 'absolute', right: '0.3em', top: '50%', width: '21px', margin: '-10px 0 0 0', padding: '1px',	height: '20px', borderRadius: '4px', cursor: 'pointer'});
	  
	  $('#ttCustomMboxHTMLTitle .tt-closethick').css({backgroundImage: 'url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-icons_888888_256x240.png)', left: '50%', marginLeft: '-8px', position: 'absolute',	top: '50%',	marginTop: '-8px', backgroundPosition: '-96px -128px', width: '16px', height: '16px', display: 'block',	textIndent: '-99999px',	overflow: 'hidden',	backgroundRepeat: 'no-repeat', cursor: 'pointer'});
	  
	  $('#ttCustomMboxHTMLTitle .tt-title-close-btn:hover .tt-closethick').css({backgroundImage: 'url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-icons_454545_256x240.png)'});

	  $('#ttCustomMboxHTMLTitle .tt-title-close-text').css({padding: '.4em',	textIndent: '-9999999px', display: 'block',	lineHeight: 'normal', color: '#555555', fontSize: '1em', cursor: 'pointer', textAlign: 'center'});
		
		//set mbxName data
		$('#ttCustomMboxHTMLContent').data('mbxName',mbxName);
		
		ttCustomMboxHTMLOverlay();
}

//mbox helper functions - END

	var c;	
	
	//Dialog placeholder
	 $("html > body").append('<div id="ttHighlDialOverlay">&nbsp;</div><div id="ttHighlDialContentWrap"><div id="ttHighlDialTitle"><span class="tt-title">T&amp;T Tools <span title="v1.1 changes: The T&T Tools Summary & highlights DO NOT show by default, you can use the [Show Summary] or [Highlight All Mboxes] buttons to do so - this allows quicker clean page overview; Added functionality to switch between default and T&T offer for any non-default mbox location; Added option to Show/Remove highlights;">(v1.1)</span></span><button class="tt-title-close-btn" role="button" title="close"><span class="tt-closethick"></span><span class="tt-title-close-text">close</span></button></div><div id="ttHighlDialContent">Summary of T&amp;T Campaigns, Experiences, Locations and Offers running on the page</div></div>');
	 
	//Dialog show/hide function
	function fnTTHighlDialOverlay() {
		$dialOverlay = $("#ttHighlDialOverlay");
		$dialContent = $("#ttHighlDialContentWrap");
		$dialOverlay.toggle();
		$dialContent.slideToggle('fast');
	}
	 
	 //Dialog Style
	 
	 $('#ttHighlDialOverlay').css({display: 'none',	position: 'fixed', left: '0px', top: '0px', width:'100%', height: ($(document).height())+'px', textAlign: 'center', zIndex: '1000', background: '#aaaaaa url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png) 50% 50% repeat-x', opacity: '0.3', filter: 'Alpha(Opacity=30)'});
	 
	  $('#ttHighlDialContentWrap').css({display: 'none', position: 'fixed', left: (Math.floor(window.innerWidth/2)-350) +'px', top: '50px', width: '700px', float: 'left',	border: '1px solid #000', padding: '3px', textAlign: 'left', fontSize: '12px', fontFamily: 'Verdana,Arial,sans-serif', borderRadius: '4px', border: '1px solid #aaaaaa', background: '#ffffff url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-bg_flat_75_ffffff_40x100.png) 50% 50% repeat-x', zIndex: '99999'});
	  
	  $('#ttHighlDialContent').css({padding: '5px', clear: 'both', lineHeight: '0.8'});
	  
	  $('#ttHighlDialTitle').css({color: '#fff', height: '20px', padding: '0.4em 1em', position: 'relative', borderRadius: '4px', fontWeight: 'bold',	border: '1px solid #aaaaaa', background: '#00aeef'});
	  
	  $('#ttHighlDialTitle .tt-title').css({float: 'left',	margin: '0.1em 0', whiteSpace: 'nowrap', width: '90%', overflow: 'hidden', textOverflow: 'ellipsis', fontWeight: 'bold', color: '#fff'});
	  
	  $('#ttHighlDialTitle .tt-title-close-btn').css({border: '1px solid #d3d3d3', background: '#e6e6e6 url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png) 50% 50% repeat-x', color: '#555555',	fontWeight:'normal', fontSize: '1em',	position: 'absolute', right: '0.3em', top: '50%', width: '21px', margin: '-10px 0 0 0', padding: '1px',	height: '20px', borderRadius: '4px', cursor: 'pointer'});
	  
	  $('#ttHighlDialTitle .tt-closethick').css({backgroundImage: 'url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-icons_888888_256x240.png)', left: '50%', marginLeft: '-8px', position: 'absolute',	top: '50%',	marginTop: '-8px', backgroundPosition: '-96px -128px', width: '16px', height: '16px', display: 'block',	textIndent: '-99999px',	overflow: 'hidden',	backgroundRepeat: 'no-repeat', cursor: 'pointer'});
	  
	  $('#ttHighlDialTitle .tt-title-close-btn:hover .tt-closethick').css({backgroundImage: 'url(http://code.jquery.com/ui/1.10.3/themes/smoothness/images/ui-icons_454545_256x240.png)'});

	  $('#ttHighlDialTitle .tt-title-close-text').css({padding: '.4em',	textIndent: '-9999999px', display: 'block',	lineHeight: 'normal', color: '#555555', fontSize: '1em', cursor: 'pointer', textAlign: 'center'});
	  
	  
	 
	
	if ($('.ttHighLLabelWrap').length>0){
		alert("Page needs to be refreshed before using the T&T Tools again. Press OK to proceed with refresh. After page is reloaded, use the tool again.");
		window.location = location.protocol + '//' + location.host + location.pathname;
	}else {
		
		/* identify element height, and if less than 10px calculate it based on child elements largest number for height */
		function ttHightBasedOnHighestChild($ttJQEl){
			ttElHeight = $ttJQEl.outerHeight();
			minH = 50;
			var highest = 0;
			 $ttJQEl.find('*:visible').each(function(){		
				var h = $(this).outerHeight();
				if(h > highest){
					highest = h;
				}
			});
			
			if (highest > ttElHeight){
				ttElHeight = highest;
			}
			
			//provide enough for the location/offer prepend data
			ttElHeight = ttElHeight+ minH;
	
			return ttElHeight;
		}
				
		//Delay to allow the above code to finish
		setTimeout(function(){
			
			/* Prepare data for dialog */
		
			var ttHighLightData = '', ttSameLastCampaignArr = [];
			
			if (typeof window.ttMETA === "undefined") {
				ttHighLightData = '<br /><br /><b class="note">No T&T Campaigns running on this page, just default!</b><br />'
			}else{
				ttHighLightData = '<b class="tthighlIntro">Summary of T&amp;T Campaigns, Experiences, Locations and Offers running on the page</b>';
				$(window.ttMETA).each(function(i, v) {					
				   
				  if ($.inArray(v.CampaignId, ttSameLastCampaignArr)==-1){
					  ttHighLightData+='<b>CampaignName: </b><a href="https://admin7.testandtarget.omniture.com/admin/campaign/campaign_edit_forward.jsp?campaignId=' + v.CampaignId +'" target="_blank" title="Click to edit this campaign (opens in a new window)"><i>' + v.CampaignName + '</i></a><br />';				 		 
				  }
				   ttHighLightData+='<b>Campaign Id: </b><a href="https://admin7.testandtarget.omniture.com/admin/campaign/campaign_edit_forward.jsp?campaignId=' + v.CampaignId +'" target="_blank" title="Click to edit this campaign (opens in a new window)"><i>' + v.CampaignId + '</i></a> | ';	
				  ttHighLightData+='<b title="RecipeName">Experience: </b><i title="Recipe Id: '+ v.RecipeId +'">' + v.RecipeName + '</i> | ';
				  ttHighLightData+='<b>Offer Name: </b><a href="#ttlabel_off_' + v.OfferId + '" title="Click to focus on the offer [OfferID: '+v.OfferId+']" onclick="fnTTHighlDialOverlay();"><i>' + v.OfferName + '</i></a><br />';
				  ttHighLightData+='<b title="MboxName">Location: </b><a href="#ttlabel_off_' + v.OfferId + '" title="Click to focus on the offer [OfferID: '+v.OfferId+']" onclick="fnTTHighlDialOverlay();"><i>' + v.MboxName + '</i></a><br /><br />';
				  
				  //add to array ttSameLastCampaignArr, so that we can group offers from the same campaign together
				  ttSameLastCampaignArr.push(v.CampaignId);
				  
				  /* Highlight all mboxes */
				  //prepend global mbox too but in a different way
				  if (v.MboxName=="Global_Mbox"){		  			  
					  $("html > body").prepend('<div class="ttHighLLabelWrap" id="ttlabel_off_'+v.OfferId+'"><div class="ttHighLMboxLabel" title="'+v.MboxName+'" style="display: none;">location: '+v.MboxName+'</div><div class="ttHighLOfferLabel" title="'+v.OfferName+'" style="display: none;">offer: '+v.OfferId+' [' + v.OfferName + '] </div></div>');
					 
				  }else{
					  //adjust height so we can have an outline
					  $("div[id*='" + v.MboxName + "']:visible:eq(0)").addClass('ttHighlMbox');
					  $("div[id*='" + v.MboxName + "']:visible:eq(0)").prepend('<div class="ttHighLLabelWrap" id="ttlabel_off_'+v.OfferId+'"><div class="ttHighLMboxLabel" title="'+v.MboxName+'"  style="display: none;">location: '+v.MboxName+'</div><div class="ttHighLOfferLabel" title="'+v.OfferName+'" data-mbName="'+v.MboxName+'" style="display: none;">offer: '+v.OfferId+' ['+v.OfferName+']</div> <div class="ttHighLOfferRestoreDefCont" data-mbName="'+v.MboxName+'" title="Click this to display the default offer" style="display: none;">Show default offer</div></div>');
				  }
				  
				});
				
				//Scan through $('div.mboxDefault') elements, if there's something additional outside window.ttMETA
				//this would help Highlight All Mboxes with offer: default
				var mboxCreateStr = '', mboxCreateStrPos = 0, mboxCreateStrLastClsBrcktPos=0, mboxCreateStrGbMbPos = 0, mboxCreateStrRnd=0 ;
				$('.mboxDefault').each(function(i, v) {
					mboxCreateStrRnd = +parseInt(Math.random()*99999999);
					mboxCreateStr = $(v).next('script').text();
					mboxCreateStrPos = mboxCreateStr.indexOf("mboxCreate");
					mboxCreateStrLastClsBrcktPos = mboxCreateStr.indexOf(")");
					if (mboxCreateStrPos > 0){
						mboxCreateStr = mboxCreateStr.substring(mboxCreateStrPos + ("mboxCreate('").length, (mboxCreateStrLastClsBrcktPos-1));
						if (!$(v).prev('div').hasClass('ttHighLLabelWrap')){
							
							$(v).addClass('ttHighlMbox');
							$(v).prepend('<div class="ttHighLLabelWrapDef" id="ttlabel_off_'+'default_rnd_'+mboxCreateStrRnd+'"><div class="ttHighLMboxLabel" title="'+mboxCreateStr+'" style="display: none;">location: '+mboxCreateStr+'</div><div class="ttHighLOfferLabel default" title="default" data-mbName="'+mboxCreateStr+'" style="display: none;">offer: '+'default'+'</div></div>');
																				
						  ttHighLightData+='<b>Offer: </b><a href="#ttlabel_off_' +'default_rnd_'+mboxCreateStrRnd+ '" title="Click to focus on the offer" onclick="fnTTHighlDialOverlay();"><i>' + 'default' + '</i></a> | ';
						  ttHighLightData+='<b title="MboxName">Location: </b><a href="#ttlabel_off_' + 'default_rnd_'+mboxCreateStrRnd+ '" title="Click to focus on the offer" onclick="fnTTHighlDialOverlay();"><i>' + mboxCreateStr + '</i></a><br /><br />';

						}
					}else{
						//no script following
						//this could be the case with a global mbox present, with default offer
						mboxCreateStrGbMbPos = 0;
						if (typeof($(v).next('div').attr('id')) !== 'undefined') {						
							mboxCreateStrGbMbPos = $(v).next('div').attr('id').indexOf('Global_Mbox');
						}
						
						if (mboxCreateStrGbMbPos>0 && !$(v).prev('div').hasClass('ttHighLLabelWrap')){
							 $("html > body").prepend('<div class="ttHighLLabelWrapDef" id="ttlabel_off_'+'default_glbl'+'"><div class="ttHighLMboxLabel" title="Global_Mbox"  style="display: none;">location: '+'Global_Mbox'+'</div><div class="ttHighLOfferLabel" title="default" style="display: none;">offer: '+ 'default' + '</div></div>');
							 
						  ttHighLightData+='<b>Offer: </b><a href="#ttlabel_off_' +'default_rnd_'+'default_glbl'+ '" title="Click to focus on the offer" onclick="fnTTHighlDialOverlay();"><i>' + 'default' + '</i></a> | ';
						  ttHighLightData+='<b title="MboxName">Location: </b><a href="#ttlabel_off_' + 'default_rnd_'+'default_glbl'+ '" title="Click to focus on the offer" onclick="fnTTHighlDialOverlay();"><i>' + mboxCreateStr + '</i></a><br /><br />';
						}
						
					}
					
				});
				
				//Add tool buttons
				ttHighLightData+='<div class="ttHighlToolBtnWrap"><div style="font-size: 11px; color: #00aeef; font-weight: bold; margin-bottom: 10px;">All of the below buttons will close this dialog and refresh the page, in addition to their own action:</div>'; //open
				ttHighLightData+='<a class="ttHighlToolBtn" id="ttHighlToolBtnRefreshPage" title="Refresh current page - removing T&T Tools highlights, compare frames, etc">Refresh Page</a>';
				ttHighLightData+='<a class="ttHighlToolBtn" id="ttHighlToolBtnDeleteTTCookie" title="This would delete mbox and mboxPC cookies, allowing you to use different ttqa parameter on the page">Delete T&amp;T Cookies</a>';
				ttHighLightData+='<a class="ttHighlToolBtn" id="ttHighlToolBtnForceDefault" title="Adds ?mboxDisable=1 to the URL to disable T&T, even Global_Mbox is disabled, i.e. switches to default. To remove, just take out ?mboxDisable=1 from the URL!">Force Default</a>';
				ttHighLightData+='<a class="ttHighlToolBtn" id="ttHighlToolBtnCompareTTvsDefault" title="Compare T&T vs Default in split screen/frames; T&T will be on the LEFT, Default on the RIGHT!">Split Screen Compare</a>';
	
	
				
				ttHighLightData+='</div>'; //close
				
			}
			
			
			$('#ttHighlDialContent').html('<p>'+ttHighLightData+'</p>');
			
			/* show dialog on load */
			// fnTTHighlDialOverlay(); //don't display on default
			
			
					/* Style dialog */	
					
					$('#ttHighlDialContent .tthighlIntro').css({color: '#00aeef', fontSize: '11px', display: 'block', marginBottom: '10px'});
					$('#ttHighlDialContent p b').css({color: '#00aeef', lineHeight: "1.7", fontSize: '11px'});
					$('#ttHighlDialContent p b.note').css({color: '#CC0000'});
					$('#ttHighlDialContent p i').css({color: '#00CC00', lineHeight: "1.7", fontSize: '11px'});
					$('#ttHighlDialContent p a i').css({cursor: 'help', lineHeight: "1.7", fontSize: '11px'});
					
					//Add tool buttons top/left fixed, outside the dialog
					$('body').append('<a id="ttoutHighlToolBtnRefreshPage" class="ttoutHighlToolBtn" style="top: 50px;" title="Refresh current page - removing T&T Tools highlights, compare frames, etc">Refresh Page</a>');
					if (typeof window.ttMETA !== "undefined") {
						$('body').append('<a id="ttoutHighlToolBtnDeleteTTCookie" class="ttoutHighlToolBtn" style="top: 80px;" title="This would delete mbox and mboxPC cookies, allowing you to use different ttqa parameter on the page">Delete T&amp;T Cookies</a>');
						$('body').append('<a id="ttoutHighlToolBtnForceDefault" class="ttoutHighlToolBtn" style="top: 110px;" title="Adds ?mboxDisable=1 to the URL to disable T&T, even Global_Mbox is disabled, i.e. switches to default. To remove, just take out ?mboxDisable=1 from the URL!">Force Default</a>');
						$('body').append('<a id="ttoutHighlToolBtnCompareTTvsDefault" class="ttoutHighlToolBtn" style="top: 140px;" title="Compare T&T vs Default in split screen/frames; T&T will be on the LEFT, Default on the RIGHT!">Split Screen Compare</a>');
						$('body').append('<a id="ttoutHighlToolBtnOpenTTSummaryInfo" class="ttoutHighlToolBtn" style="top: 170px;" title="Show Summary of Campaigns, Experiences, Locations, Offers">Show Summary</a>');
						$('body').append('<a id="ttoutHighlToolBtnShowHideHighlights" class="ttoutHighlToolBtn show" style="top: 200px;" title="Highlight mbox-es/Locations with outlines and info">Highlight All Mboxes</a>');
						$('body').append('<input type="checkbox" name="ttMouseHighlight" id="ttMouseHighlight" value="mouseHighlight" style="top: 230px;"><label for="ttMouseHighlight" style="top: 226px;" id="ttMouseHighlightLabel" title="When this is checked, you can Highlight and see Mbox info on mouse over.">Mouse Highlights</label>');
					}else {
						$('body').append('<a id="ttoutHighlToolBtnOpenTTSummaryInfo" class="ttoutHighlToolBtn" style="top: 80px;" title="Bring up the T&T Tools summary screen again">Show Summary</a>');
					}
	
					
					//Style tool buttons
					$(".ttHighlToolBtn").css({backgroundColor: '#00aeef', borderRadius: '7px', color: '#fff', cursor: 'pointer', display: 'inline-block', fontWeight: 'bold', height: '18px', padding: '5px 10px 2px', margin: '5px 10px 5px 0', width: 'auto', fontSize: '11px' });

					$(".ttoutHighlToolBtn").css({backgroundColor: '#00aeef', borderRadius: '7px', color: '#fff', cursor: 'pointer', display: 'inline-block', fontWeight: 'bold', height: '16px', padding: '5px 10px 2px', margin: '5px 10px 5px 0', width: 'auto', fontSize: '10px', position: 'fixed', left: '8px' });
					$('#ttoutHighlToolBtnOpenTTSummaryInfo').css({backgroundColor: '#ff8c00'});
					$('#ttoutHighlToolBtnShowHideHighlights').css({backgroundColor: '#ff0000'});
					$('#ttMouseHighlight').css({ display: 'inline-block', margin: '5px 10px 5px 0', position: 'fixed', left: '12px' });
					$('#ttMouseHighlightLabel').css({display: 'inline-block', fontWeight: 'bold', padding: '5px 10px 2px', margin: '5px 10px 5px 0', fontSize: '10px', position: 'fixed', left: '20px' });
					
					//Bind events to tool buttons
					$("#ttHighlToolBtnRefreshPage, #ttoutHighlToolBtnRefreshPage").bind('click',function(e){
						window.location = location.protocol + '//' + location.host + location.pathname;
						return false;
					});
					$("#ttHighlToolBtnDeleteTTCookie, #ttoutHighlToolBtnDeleteTTCookie").bind('click',function(e){
						document.cookie='mbox'+'='+(('/')?';path=/':'')+(('.barclays.co.uk')?';domain='+'.barclays.co.uk':'')+';expires=Thu, 01-Jan-1970 00:00:01 GMT';document.cookie='mboxPC'+'='+(('/')?';path=/':'')+(('.barclays.co.uk')?';domain='+'.barclays.co.uk':'')+';expires=Thu, 01-Jan-1970 00:00:01 GMT';document.cookie='mboxSession'+'='+(('/')?';path=/':'')+(('.barclays.co.uk')?';domain='+'.barclays.co.uk':'')+';expires=Thu, 01-Jan-1970 00:00:01 GMT';	
						window.location = location.protocol + '//' + location.host + location.pathname;
						return false;
					});
					$("#ttHighlToolBtnForceDefault, #ttoutHighlToolBtnForceDefault").bind('click',function(e){
						window.location = location.protocol + '//' + location.host + location.pathname + '?mboxDisable=1';
						return false;
					});
					
					$("#ttHighlToolBtnCompareTTvsDefault, #ttoutHighlToolBtnCompareTTvsDefault").bind('click',function(e){
						 var fs = document.createElement('frameset'),
							f1 = document.createElement('frame'),
							f2 = document.createElement('frame'),
							mD1s=(window.location.href.indexOf('?')!=-1)?'&':'?';
							
							fs.cols = "*,*";
							fs.framespacing = "0";
							f1.name = "topframe";
							f1.src = window.location.href + mD1s + "rnd=" + parseInt(Math.random()*99999999);
							f1.marginwidth = "0";
							f1.marginheight = "0";
							/*f1.noresize = "noresize";*/
							f1.scrolling = "auto";
							f2.name = "bottomframe";
							f2.src = window.location.href + mD1s + "mboxDisable=1";
							f2.marginwidth = "0";
							f2.marginheight = "0";
							f2.scrolling = "auto";
							f2.frameborder = "0";
							fs.appendChild(f1);
							fs.appendChild(f2);
							$("body").replaceWith(fs);

						return false;
					});
					
					$('#ttHighlDialTitle .tt-title-close-btn, #ttoutHighlToolBtnOpenTTSummaryInfo').live('click',function(e){
						e.preventDefault();
						fnTTHighlDialOverlay();
					});
					
					$('#ttoutHighlToolBtnShowHideHighlights.show').live('click',function(e){
						e.preventDefault();
						
						$('.ttHighLMboxLabel, .ttHighLOfferLabel, .ttHighLOfferRestoreDefCont').show();
						$(".ttHighLLabelWrapDef").css({backgroundColor: '#00aeef', color: '#fff', padding: '5px'});
						$(".ttHighLLabelWrap").css({backgroundColor: '#00aeef', color: '#fff', padding: '5px'});
						$('div.ttHighlMbox').css({border: '3px outset red', clear: 'both', width: '101%'});
						$('div.ttHighlMbox:visible').each(function(i,e){
							$(this).height(ttHightBasedOnHighestChild($(this))+30+'px');	
						});
						$(this).removeClass('show').addClass('remove').attr('title','Remove mbox-es/Locations highlight outlines and info').html('Remove Highlights');
						$('#ttMouseHighlight').hide();
						$('#ttMouseHighlightLabel').hide();
					});
					
					$('#ttoutHighlToolBtnShowHideHighlights.remove').live('click',function(e){
						e.preventDefault();
						$('.ttHighLMboxLabel, .ttHighLOfferLabel, .ttHighLOfferRestoreDefCont').hide();
						$('.ttHighLLabelWrapDef').removeAttr('style').css('visibility','visible');
						$('.ttHighLLabelWrap').removeAttr('style').css('visibility','visible');
						$('.ttHighlMbox').removeAttr('style').css('visibility','visible');
						$(this).removeClass('remove').addClass('show').attr('title','Highlight mbox-es/Locations with outlines and info').html('Highlight All Mboxes');
						$('#ttMouseHighlight').show();
						$('#ttMouseHighlightLabel').show();
					});
					
					$('#ttCustomMboxHTMLTitle .tt-title-close-btn').live('click',function(e){
						e.preventDefault();
						ttCustomMboxHTMLOverlay();
					});
					
					document.onkeydown = function(evt) {
						evt = evt || window.event;
						if (evt.keyCode == 27) { //Esc key
							if ($("#ttHighlDialOverlay").is(':visible')){
								evt.preventDefault();
								fnTTHighlDialOverlay();
							}
							if ($("#ttCustomMboxHTMLOverlay").is(':visible')){
								evt.preventDefault();
								ttCustomMboxHTMLOverlay();
							}
						}
					};
					
					//Additional binds
					
					//restore default for certain mbox
					$('.ttHighLOfferRestoreDefCont').css({cursor: 'pointer', textDecoration: 'underline', color: '#ffff00'});
					$('.ttHighLOfferRestoreDefCont').live('click',function(e){
						if(e.altKey){
							fnTTMboxLoadCustom($(this).attr('data-mbName'));
						}else{
							fnTTMboxLoadDef($(this).attr('data-mbName'));
						}
						
					});
					
					$('.ttHighLOfferLabel.default').live('click',function(e){
						if(e.altKey){
							fnTTMboxLoadCustom($(this).attr('data-mbName'));
						}
					});
					
					$('#ttCustomMboxHTMLContent button').live('click',function(e){
							//Edit HTML for particular mbox on match
							mboxFactoryDefault.getMboxes().each(function(i,e) { 
								if (i.getName()==$('#ttCustomMboxHTMLContent').data('mbxName')) {
									$(i.getDiv()).html($('#ttCustomMboxHTMLContent textarea').val());
								}
							});
							ttCustomMboxHTMLOverlay();
					});
					
					$('.ttHighlMbox').live('mouseenter',function(e){
						if ($('#ttoutHighlToolBtnShowHideHighlights.show').length==1 && $('#ttMouseHighlight').is(':checked')){ //only when highlights are not on, already
							$(this).find('.ttHighLMboxLabel, .ttHighLOfferLabel, .ttHighLOfferRestoreDefCont').show();
							$(this).find(".ttHighLLabelWrapDef").css({backgroundColor: '#00aeef', color: '#fff', padding: '5px'});
							$(this).find(".ttHighLLabelWrap").css({backgroundColor: '#00aeef', color: '#fff', padding: '5px'});						
							$(this).css({border: '3px outset red', clear: 'both', width: '101%'});
							$(this).height(ttHightBasedOnHighestChild($(this))+30+'px');
						}
						
					}).live('mouseleave',function(e){
						if ($('#ttoutHighlToolBtnShowHideHighlights.show').length==1 && $('#ttMouseHighlight').is(':checked')){ //only when highlights are not on, already
							$(this).find('.ttHighLMboxLabel, .ttHighLOfferLabel, .ttHighLOfferRestoreDefCont').hide();
							$(this).find('.ttHighLLabelWrapDef').removeAttr('style').css('visibility','visible');
							$(this).find('.ttHighLLabelWrap').removeAttr('style').css('visibility','visible');						
							$(this).removeAttr('style').css('visibility','visible');
						}
					})
		},1000);
	}